package com.ies.ieschool.client.view;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuBar;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.MenuItemSeparator;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

public class ApplicationMenuView extends HLayout {

	private static final int APPLICATION_MENU_HEIGHT = 27;
	private static final int DEFAULT_SHADOW_DEPTH = 10;
	private static final int DEFAULT_MENU_WIDTH = 70;

	// Main Menu
	private String[] file_th = { "เปิดแท๊บใหม่", "เปิดหน้าต่างใหม่","ออกจากระบบ" };
	private String[] view_th = { "เปลี่ยนภาษา", "เปลี่ยนขนาดฟ้อนท์" };
	private String[] help_th = { "วิธีการใช้", "การช่วยเหลือเบื้องต้น","แจ้งข้อผิดพลาด", "เกี่ยงกับ IESCHOOL" };

	// Sub Menu
	private String[] view_sub_lang = { "ภาษาไทย", "Bahasa melayu", "English", "العربية" };
	private String[] view_sub_font = { "12px", "14px", "16px", "18px" };
	

	// Icon for Main Menu
	private String[] file_icon = { "new_tab", "new_window", "logout" };
	private String[] view_icon = { "language", "font" };
	private String[] help_icon = { "help", "initial_help", "bug", "ieschool" };
	
	// Icon for Sub Menu
	private String[] view_sub_lang_icon = {"thai","malay","english","arabic"};
	private String[] view_sub_font_icon = {"font_size","font_size","font_size","font_size"};

	private static final String SEPARATOR = "separator";
	private static String ICON_PREFIX = "icons/application_menu/";
	private static final String ICON_SUFFIX = ".png";

	private MenuBar menuBar;
	private int menuPosition = 0;

	public ApplicationMenuView() {
		super();

		GWT.log("init ApplicationMenu()...", null);

		// initialise the Application Menu layout container
		this.addStyleName("applicationMenu");
		this.setHeight(APPLICATION_MENU_HEIGHT);

		menuBar = new MenuBar();

		// add the Menu Bar to the Application Menu layout container
		this.addMember(menuBar);

		addMenu("ไ<u>ฟ</u>ล์", file_th, file_icon, DEFAULT_MENU_WIDTH, new ApplicationMenuClickHandler());
		Menu viewMenu = addMenu("<u>มุ</u>มมอง", DEFAULT_MENU_WIDTH);
		addSubMenu(viewMenu, view_th[0], view_icon[0], view_sub_lang,view_sub_lang_icon, new ApplicationMenuClickHandler());
		addSubMenu(viewMenu, view_th[1], view_icon[1], view_sub_font,view_sub_font_icon, new ApplicationMenuClickHandler());

		addMenu("<u>ช่</u>วยเหลือ", help_th, help_icon, DEFAULT_MENU_WIDTH,
				new ApplicationMenuClickHandler());

	}

	public Menu addMenu(String menuName, String[] menuItems, String[] menuIcon,
			int width, ClickHandler clickHandler) {
		// initialise the new menu
		Menu menu = new Menu();
		menu.setTitle(menuName);
		menu.setShowShadow(true);
		menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);
		menu.setWidth(width);

		for (int i = 0; i < menuItems.length; i++) {
			// remove any whitespace
			String menuItemName = menuItems[i].replaceAll("\\W", "");

			if (menuItemName.contentEquals(SEPARATOR)) {
				MenuItemSeparator separator = new MenuItemSeparator();
				menu.addItem(separator);
				continue;
			}

			MenuItem menuItem = new MenuItem(menuItems[i], getIcon(menuIcon[i]));

			menuItem.addClickHandler(clickHandler);
			menu.addItem(menuItem);
		}

		Menu[] menus = new Menu[1];
		menus[0] = menu;
		menuBar.addMenus(menus, menuPosition);
		menuPosition++;

		return menus[0];
	}

	public Menu addMenu(String menuName, int width) {
		// initialise the new menu
		Menu menu = new Menu();
		menu.setTitle(menuName);
		menu.setShowShadow(true);
		menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);
		menu.setWidth(width);

		Menu[] menus = new Menu[1];
		menus[0] = menu;
		menuBar.addMenus(menus, menuPosition);
		menuPosition++;

		return menu;
	}

	public Menu addSubMenu(Menu parentMenu, String subMenuName,
			String subMenuIcon, String[] menuItems, String[] menuIcon,
			ClickHandler clickHandler) {
		// initialise the new sub menu
		Menu menu = new Menu();
		menu.setShowShadow(true);
		menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);

		MenuItem menuItem = new MenuItem(subMenuName);

		// create an array of menu item names
		for (int i = 0; i < menuItems.length; i++) {
			// remove any whitespace
			String menuItemName = menuItems[i].replaceAll("\\W", "");

			if (menuItemName.contentEquals(SEPARATOR)) {
				MenuItemSeparator separator = new MenuItemSeparator();
				menu.addItem(separator);
				continue;
			}

			menuItem = new MenuItem(menuItems[i], getIcon(menuIcon[i]));
			menuItem.addClickHandler(clickHandler);
			menu.addItem(menuItem);
		}

		// add the sub menu to the menu item
		menuItem = new MenuItem(subMenuName,getIcon(subMenuIcon));
		parentMenu.addItem(menuItem);
		menuItem.setSubmenu(menu);

		return menu;
	}

	public final static String DELIMITER = ",";

	public static String[] splitMenu(String line) {
		return line.split(DELIMITER);
	}

	private String getIcon(String applicationName) {
		// remove any whitespace
		String name = applicationName.replaceAll("\\W", "");
		// e.g. "icons/16/" + "activities" + ".png"
		String icon = ICON_PREFIX + name.toLowerCase() + ICON_SUFFIX;
		GWT.log(icon, null);

		return icon;
	}

	private class ApplicationMenuClickHandler implements ClickHandler {
		@Override
		public void onClick(MenuItemClickEvent event) {
			String applicationName = event.getItem().getTitle();
			SC.say("You clicked: " + applicationName);
		}
	}
}